package gateways

import (
	"encoding/json"
	"fmt"
	"gitee.com/haifengat/goctp"
	// ctp "gitee.com/haifengat/goctp/lnx"
	ctp "gitee.com/haifengat/goctp/win"
)

func NewCtpGateway() *CtpGateway {
	gateway := new(CtpGateway)
	gateway.Exchanges = []string{"CFFEX", "CFFEX", "CZCE", "DCE", "INE", "GFEX"}
	gateway.Quote = ctp.NewQuote()
	gateway.Trade = ctp.NewTrade()
	return gateway
}

type CtpGateway struct {
	Exchanges  []string //交易所列表
	tradeFront string
	quoteFront string
	brokerID   string
	investorID string
	password   string
	appID      string
	authCode   string
	Trade      *ctp.Trade
	Quote      *ctp.Quote
}

func (ctpGateway CtpGateway) Connect(setting map[string]string) {
	ctpGateway.tradeFront = "tcp://" + setting["交易服务器"]
	ctpGateway.quoteFront = "tcp://" + setting["行情服务器"]
	ctpGateway.brokerID = setting["经纪商代码"]
	ctpGateway.investorID = setting["用户名"]
	ctpGateway.password = setting["密码"]
	ctpGateway.appID = setting["产品名称"]
	ctpGateway.authCode = setting["授权编码"]

	ctpGateway.testQuote()
	ctpGateway.testTrade()
}

func (ctpGateway CtpGateway) testQuote() {
	ctpGateway.Quote.RegOnFrontConnected(func() {
		fmt.Println("quote connected")
		ctpGateway.Quote.ReqLogin(ctpGateway.investorID, ctpGateway.password, ctpGateway.brokerID)
	})
	ctpGateway.Quote.RegOnRspUserLogin(func(login *goctp.RspUserLoginField, info *goctp.RspInfoField) {
		fmt.Println("quote login:", info)
	})
	ctpGateway.Quote.RegOnTick(func(tick *goctp.TickField) {
		bs, _ := json.Marshal(tick)
		fmt.Println(string(bs))
	})
	fmt.Println("quote connected:" + ctpGateway.quoteFront)
	ctpGateway.Quote.ReqConnect(ctpGateway.quoteFront)
}

func (ctpGateway CtpGateway) testTrade() {
	ctpGateway.Trade.RegOnFrontConnected(func() {
		fmt.Println("trade connected")
		go ctpGateway.Trade.ReqLogin(ctpGateway.investorID, ctpGateway.password, ctpGateway.brokerID, ctpGateway.appID, ctpGateway.authCode)
	})
	ctpGateway.Trade.RegOnRspUserLogin(func(login *goctp.RspUserLoginField, info *goctp.RspInfoField) {
		fmt.Println(info)
		bs, _ := json.Marshal(login)
		fmt.Println("login: ", string(bs))
	})

	ctpGateway.Trade.RegOnRtnOrder(func(field *goctp.OrderField) {
		bs, _ := json.Marshal(field)
		fmt.Println("OnRtnOrder:", string(bs))
	})
	ctpGateway.Trade.RegOnRtnTrade(func(field *goctp.TradeField) {
		bs, _ := json.Marshal(field)
		fmt.Println("OnRtnTrade:", string(bs))
	})
	ctpGateway.Trade.RegOnRtnCancel(func(field *goctp.OrderField) {
		bs, _ := json.Marshal(field)
		fmt.Println("OnRtnCancel: ", string(bs))
	})
	ctpGateway.Trade.RegOnErrRtnOrder(func(field *goctp.OrderField, info *goctp.RspInfoField) {
		bs, _ := json.Marshal(info)
		fmt.Println("OnErrRtnOrder: ", string(bs))
	})
	// 交易状态
	ctpGateway.Trade.RegOnRtnInstrumentStatus(func(field *goctp.InstrumentStatus) {
		// fmt.Println(field)
	})
	// 断开
	ctpGateway.Trade.RegOnFrontDisConnected(func(reason int) {
		fmt.Println("disconntcted: ", reason)
	})
	fmt.Println("trade connected:" + ctpGateway.tradeFront)
	ctpGateway.Trade.ReqConnect(ctpGateway.tradeFront)
}
